package org.swimv2.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneLavoro;
import org.swimv2.controller.GestioneLavoroRemote;
import org.swimv2.exception.FeedbackNonValidoException;
import org.swimv2.model.Feedback;
import org.swimv2.model.Lavoro;
import org.swimv2.model.Persona;

public class RilasciaFeedbackServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Persona u = (Persona) request.getSession().getAttribute("utente");
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneLavoro.JNDI_NAME);
			GestioneLavoroRemote manager = (GestioneLavoroRemote)PortableRemoteObject.narrow(obj,GestioneLavoroRemote.class);
			long idLavoroTemp = (long)request.getSession().getAttribute("idLavoroTemp");
			Lavoro lavoro = manager.dettagliLavoro(idLavoroTemp);
			request.getSession().setAttribute("lavoroTemp",lavoro);
			Persona boss = lavoro.getPersona();
			Collection <Persona> membri = manager.getPartecipantiLavoro(String.valueOf(idLavoroTemp));
			request.getSession().setAttribute("membri", membri);	
			request.getRequestDispatcher("WEB-INF/rilasciaFeedback.jsp").
			forward(request, response);			

		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
	}
	
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String IDdestinatario = request.getParameter("destinatarioFeedback");
        if(IDdestinatario==null)
        {
        	request.setAttribute("messaggio","Selezionare destinatario per il feedback");
        	request.getRequestDispatcher("WEB-INF/rilasciaFeedback.jsp").
			forward(request, response);		
        }
        Persona u = (Persona) request.getSession().getAttribute("utente");
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneLavoro.JNDI_NAME);
			GestioneLavoroRemote manager = (GestioneLavoroRemote)PortableRemoteObject.narrow(obj,GestioneLavoroRemote.class);
			long idLavoroTemp = (long)request.getSession().getAttribute("idLavoroTemp");
			Lavoro lavoro = manager.dettagliLavoro(idLavoroTemp);
			String descrizione = request.getParameter("descrizione");
			Persona boss = lavoro.getPersona();
			String bossID = boss.getUserID();
			byte valutazioneNum =(byte) Integer.parseInt (request.getParameter("valutazione"));
			Collection <Persona> membri = manager.getPartecipantiLavoro(String.valueOf(idLavoroTemp));
			Persona persona =null;
			for(Persona p : membri) {
				if(p.getUserID().equals(IDdestinatario)) { persona=p;}
			}
			
			Feedback feedback = new Feedback();
			feedback.setLavoro(lavoro);
			feedback.setDescrizione(descrizione);
			feedback.setPersona(persona);
			feedback.setUserIDmittente(bossID);
			feedback.setValutazione_num_(valutazioneNum);
			try{
			manager.rilasciaFeedback(feedback);
			request.setAttribute("messaggio2", "Feedback rilasciato con successo");
			}
			catch(FeedbackNonValidoException e){
				request.setAttribute("messaggio", e.getError());
			}
			request.getRequestDispatcher("WEB-INF/rilasciaFeedback.jsp").
			forward(request, response);			

		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
	}

}
